package com.wu.foodsafetyplayer.app;

import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.wu.foodsafetyplayer.R;
import com.wu.foodsafetyplayer.app.fragment.main.BeautifulFragment;
import com.wu.foodsafetyplayer.app.fragment.main.GovernmentFragment;
import com.wu.foodsafetyplayer.app.fragment.main.HomeFragment;
import com.wu.foodsafetyplayer.app.fragment.main.MonitorFragment;
import com.wu.foodsafetyplayer.app.fragment.viewpager.MyHouseFragment;
import com.wu.foodsafetyplayer.app.fragment.main.PromoteFragment;
import com.wu.foodsafetyplayer.app.fragment.main.SafetyFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

public class AppMainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
    private List<Fragment> mFragments = new ArrayList<>();
    public static int mCurrentIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_activity_main);
        initView();
    }

    public void initView() {
        BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);

        mFragments.add(new MonitorFragment());
        mFragments.add(new GovernmentFragment());
        mFragments.add(new SafetyFragment());
        mFragments.add(new BeautifulFragment());
        mFragments.add(new PromoteFragment());

        getSupportFragmentManager().beginTransaction()
                .add(R.id.layout_content, mFragments.get(0))
                .commitAllowingStateLoss();

        mCurrentIndex = 0;
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int index;
        int itemId = menuItem.getItemId();
        switch (itemId) {
            default:
            case R.id.tab_1:
                index = 0;
                break;
            case R.id.tab_2:
                index = 1;
                break;
            case R.id.tab_3:
                index = 2;
                break;
            case R.id.tab_4:
                index = 3;
                break;
            case R.id.tab_5:
                index = 4;
                break;
        }

        if (mCurrentIndex != index) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            Fragment fragment = mFragments.get(index);
            Fragment curFragment = mFragments.get(mCurrentIndex);
            if (fragment.isAdded()) {
                transaction.hide(curFragment).show(fragment);
            } else {
                transaction.add(R.id.layout_content, fragment).hide(curFragment);
            }
            transaction.commitAllowingStateLoss();
            mCurrentIndex = index;
        }
        return true;
    }
}
